package com.book.lizhi.widget;

import com.book.lizhi.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class AppTitleView extends FrameLayout {

	private String leftText;
	private String rightText;
	private String centerText;

	private Drawable leftIcon;
	private Drawable rightIcon;
	private Drawable backgroudDrawable;

	private TextView mLeftTextView;
	private TextView mRightTextView;
	private TextView mCenterTextView;

	private ImageView leftIconView;
	private ImageView rightIconView;

	private View mLeftView;
	private View mRightView;

	private View mLeftContainer;
	private View mRightContainer;

	private OnTitleListener onTitleListener;

	public interface OnTitleListener {
		public void onLeft(View view);

		public void onRight(View view);
	}

	public AppTitleView(Context context) {
		super(context);
		initView(context);
	}

	public AppTitleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppTitleView);
		leftText = a.getString(R.styleable.AppTitleView_left_text);
		leftIcon = a.getDrawable(R.styleable.AppTitleView_left_icon);
		rightText = a.getString(R.styleable.AppTitleView_right_text);
		rightIcon = a.getDrawable(R.styleable.AppTitleView_right_icon);
		centerText = a.getString(R.styleable.AppTitleView_center_text);
		backgroudDrawable=a.getDrawable(R.styleable.AppTitleView_backgroud);
		a.recycle();
		initView(context);
	}

	@SuppressWarnings("deprecation")
	private void initView(Context context) {
		inflate(context, R.layout.app_title_layout, this);

		mLeftContainer = findViewById(R.id.left_container);
		mRightContainer = findViewById(R.id.right_contaner);

		mLeftTextView = (TextView) findViewById(R.id.app_title_left);
		mRightTextView = (TextView) findViewById(R.id.app_title_right);
		mCenterTextView = (TextView) findViewById(R.id.app_title_center);

		leftIconView = (ImageView) findViewById(R.id.app_title_left_icon);
		rightIconView = (ImageView) findViewById(R.id.app_title_right_icon);

		mLeftContainer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (onTitleListener != null && mLeftView != null) {
					onTitleListener.onLeft(mLeftView);
				}
			}
		});

		mRightContainer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (onTitleListener != null && mRightView != null) {
					onTitleListener.onRight(mRightView);
				}
			}
		});

		if (!TextUtils.isEmpty(leftText)) {
			mLeftTextView.setText(leftText);
			mLeftTextView.setVisibility(View.VISIBLE);
			mLeftView = mLeftTextView;
		} else {
			mLeftTextView.setVisibility(View.GONE);
		}

		if (!TextUtils.isEmpty(rightText)) {
			mRightTextView.setText(rightText);
			mRightTextView.setVisibility(View.VISIBLE);
			mRightView = mRightTextView;
		} else {
			mRightTextView.setVisibility(View.GONE);
		}

		if (!TextUtils.isEmpty(centerText)) {
			mCenterTextView.setVisibility(View.VISIBLE);
			mCenterTextView.setText(centerText);
		} else {
			mCenterTextView.setVisibility(View.GONE);
		}

		if (leftIcon != null) {
			leftIconView.setVisibility(View.VISIBLE);
			leftIconView.setImageDrawable(leftIcon);
			mLeftView = leftIconView;
		} else {
			leftIconView.setVisibility(View.GONE);
		}

		if (rightIcon != null) {
			rightIconView.setVisibility(View.VISIBLE);
			rightIconView.setImageDrawable(rightIcon);
			mRightView = rightIconView;
		} else {
			rightIconView.setVisibility(View.GONE);
		}
		
		if(backgroudDrawable!=null){
			findViewById(R.id.title_container).setBackgroundDrawable(backgroudDrawable);
		}else{
			findViewById(R.id.title_container).setBackgroundResource(R.drawable.headdian_x);
		}
	}

	public OnTitleListener getOnTitleListener() {
		return onTitleListener;
	}

	public void setOnTitleListener(OnTitleListener onTitleListener) {
		this.onTitleListener = onTitleListener;
	}

}
